/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:59 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class KiemtraDAL : SqlProvider<KiemtraEntity>
    {
        static KiemtraDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                KiemtraEntity entity = new KiemtraEntity();
				entity.PK_iKiemtraID = Int64.Parse("0"+dr["PK_iKiemtraID"].ToString());
				entity.sTenbaikiemtra = dr["sTenbaikiemtra"].ToString();
				entity.FK_iKhoahocID = Int64.Parse("0"+dr["FK_iKhoahocID"].ToString());
				entity.FK_iDekiemtraID = Int64.Parse("0"+dr["FK_iDekiemtraID"].ToString());
				entity.FK_iHinhthuckiemtraID = Int16.Parse("0"+dr["FK_iHinhthuckiemtraID"].ToString());
				entity.FK_iDanhmuckiemtraID = Int16.Parse("0"+dr["FK_iDanhmuckiemtraID"].ToString());
				entity.iTrangthai = Int16.Parse("0"+dr["iTrangthai"].ToString());
				entity.dNgaykiemtra =String.IsNullOrEmpty(dr["dNgaykiemtra"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaykiemtra"].ToString());
				entity.dGiobatdaukiemtra =String.IsNullOrEmpty(dr["dGiobatdaukiemtra"].ToString())?DateTime.Now:DateTime.Parse(dr["dGiobatdaukiemtra"].ToString());
				entity.iThoigianlambai = Int16.Parse("0"+dr["iThoigianlambai"].ToString());
                return entity;
            };
        }
        public static KiemtraEntity GetOne(Int64 PK_iKiemtraID)
        {
            string cmdName = "spKiemtra_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iKiemtraID", PK_iKiemtraID);
            KiemtraEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<KiemtraEntity> GetAll()
        {
            string cmdName = "spKiemtra_Get";
            return GetList(cmdName);
        }
        public static List<KiemtraEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
		{
			string cmdName = "spKiemtra_GetByFK_FK_iKhoahocID";
			SqlParameter p = new SqlParameter("@FK_iKhoahocID",FK_iKhoahocID);
			List<KiemtraEntity> list = GetList(cmdName, p);
			return list;
		}public static List<KiemtraEntity> GetByFK_iDekiemtraID(Int64 FK_iDekiemtraID)
		{
			string cmdName = "spKiemtra_GetByFK_FK_iDekiemtraID";
			SqlParameter p = new SqlParameter("@FK_iDekiemtraID",FK_iDekiemtraID);
			List<KiemtraEntity> list = GetList(cmdName, p);
			return list;
		}public static List<KiemtraEntity> GetByFK_iHinhthuckiemtraID(Int16 FK_iHinhthuckiemtraID)
		{
			string cmdName = "spKiemtra_GetByFK_FK_iHinhthuckiemtraID";
			SqlParameter p = new SqlParameter("@FK_iHinhthuckiemtraID",FK_iHinhthuckiemtraID);
			List<KiemtraEntity> list = GetList(cmdName, p);
			return list;
		}public static List<KiemtraEntity> GetByFK_iDanhmuckiemtraID(Int16 FK_iDanhmuckiemtraID)
		{
			string cmdName = "spKiemtra_GetByFK_FK_iDanhmuckiemtraID";
			SqlParameter p = new SqlParameter("@FK_iDanhmuckiemtraID",FK_iDanhmuckiemtraID);
			List<KiemtraEntity> list = GetList(cmdName, p);
			return list;
		}
     
        public static int Add(KiemtraEntity entity)
        {
            string cmdName = "spKiemtra_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static bool Edit(KiemtraEntity entity)
        {
            string cmdName = "spKiemtra_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }
        public static bool Remove(Int64 PK_iKiemtraID)
        {
            string cmdName = "spKiemtra_Delete";
            SqlParameter p = new SqlParameter("@PK_iKiemtraID", PK_iKiemtraID);
            return Run(cmdName,false,p)>0;
        }
        #region private
        private static SqlParameter[] initParams(KiemtraEntity entity)
        {
            SqlParameter[] p = new SqlParameter[10];
			p[0] = new SqlParameter("@PK_iKiemtraID", entity.PK_iKiemtraID);
			p[1] = new SqlParameter("@sTenbaikiemtra", entity.sTenbaikiemtra);
			p[2] = new SqlParameter("@FK_iKhoahocID", entity.FK_iKhoahocID);
			p[3] = new SqlParameter("@FK_iDekiemtraID", entity.FK_iDekiemtraID);
			p[4] = new SqlParameter("@FK_iHinhthuckiemtraID", entity.FK_iHinhthuckiemtraID);
			p[5] = new SqlParameter("@FK_iDanhmuckiemtraID", entity.FK_iDanhmuckiemtraID);
			p[6] = new SqlParameter("@iTrangthai", entity.iTrangthai);
			p[7] = new SqlParameter("@dNgaykiemtra", entity.dNgaykiemtra);
			p[8] = new SqlParameter("@dGiobatdaukiemtra", entity.dGiobatdaukiemtra);
			p[9] = new SqlParameter("@iThoigianlambai", entity.iThoigianlambai);
            return p;
        }
        #endregion
       
    }
}